"""
Author: wenbin
Create: 2024-08-15
"""
from flask import Flask

import app.setup as setup
from app.api import v1
from app.exception import global_exception_handler
from app.libs import migrate
from app.models.base_model import db


def create_app() -> Flask:
    """创建实例"""
    app = Flask(__name__)
    # 加载配置
    app.config.from_object(setup)
    app.register_error_handler(Exception, global_exception_handler)
    _register_blueprint(app)
    _register_plugin(app)
    return app


def _register_blueprint(app: Flask) -> None:
    """注册蓝图"""
    v1.register_blueprint(app)


def _register_plugin(app: Flask) -> None:
    """注册插件"""
    # 初始化数据库
    db.init_app(app)
    # 初始化数据库迁移工具
    migrate.init_app(app, db)
